#include<vector>
#include<queue>
#include<fstream>
#include<iostream>

using namespace std;

int main() {

	ifstream input("in.txt", ifstream::in);
    if (!input.is_open()) {
        cout<<"No input file!\n";
        system("pause");
        exit(1);
    }
    
    int n = 0;
    input>>n;

	vector<vector<int>> Gr(n);
	for(int i = 0; i < n; ++i) {
		Gr[i].resize(n,0);
	}
    
    int a, b;
    while (input.good()) {
        input>>a;
        if (a == EOF) break;
        input>>b;
        Gr[a][b] = 1;
    }

	for(int i = 0; i < n; ++i) {
		queue<int> q;
		vector<int> verts;
		vector<bool> visited(n);
		q.push(i);
		visited[i] = true;
		int tmp;
		while(!q.empty()) {
			tmp = q.front();
			q.pop();
			for(int i = 0; i < n; ++i)
				if(Gr[tmp][i] == 1)
					if(!visited[i]) {
						q.push(i);
						verts.push_back(i);
						visited[i] = true;
					}
		}
		std::sort(verts.begin(),verts.end());
		for(std::vector<int>::const_iterator it = verts.begin(); it != verts.end(); ++it)
			cout<<i<<' '<<*it<<endl;
	}
	system("pause");


	return 0;
}